package com.ymatou.iapi.couponmanage.testcase;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.coupon.service.UserReceiveCouponCall;
import com.ymatou.iapi.couponmanage.parameter.CouponInfo;
import com.ymatou.iapi.couponmanage.parameter.CreateCouponBean;
import com.ymatou.iapi.couponmanage.parameter.GetUserCouponInfoesByPagingBean;
import com.ymatou.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.ymatou.iapi.couponmanage.service.CreateCouponCall;
import com.ymatou.iapi.couponmanage.service.GetUserCouponInfoesByPagingCall;
import com.ymatou.iapi.couponmanage.service.SellerCreateBatchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponServiceMCallServiceV2;
import com.ymttest.database.model.coupon;
import com.ymttest.database.model.couponbatch;
import com.ymttest.database.sqlwapper.couponWapper;
import com.ymttest.database.sqlwapper.couponbatchWapper;
import com.ymttest.utils.TestDataManager;
//import com.ymttest.database.model.couponbatch;
//import com.ymttest.database.model.coupon;
//import com.ymttest.database.model.YmtCouponSetting;
//import com.ymttest.database.model.YmtCouponValue;
//import com.ymttest.database.sqlwapper.couponbatchWapper;
//import com.ymttest.database.sqlwapper.couponWapper;
//import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
//import com.ymttest.database.sqlwapper.YmtCouponValueWapper;
//import com.ymttest.database.sqlwapper.YmtCouponWapper;
import com.ymttest.utils.YMTDateUtil;

import javafx.scene.chart.PieChart.Data;
/**
*获取用户优惠券信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetUserCouponInfoesByPaging{
	private static GetUserCouponInfoesByPagingBean getusercouponinfoesbypagingBean;
	private static GetUserCouponInfoesByPagingCall getusercouponinfoesbypagingCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取用户优惠券信息");
	}

	@Before
	public void caseUp() {
		getusercouponinfoesbypagingBean=new GetUserCouponInfoesByPagingBean();
		getusercouponinfoesbypagingCall=new GetUserCouponInfoesByPagingCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getusercouponinfoesbypagingCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetUserCouponInfoesByPaging_001() {
		Logger.start(true,"获取用户优惠券信息");
		try {

			coupon coupon = new couponWapper().selectTop1();
			couponbatch batch = new couponbatchWapper().selectByPrimaryKey(coupon.getBatchId());
		//	YmtCouponSetting setting = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			
			getusercouponinfoesbypagingBean.setUserId(coupon.getUserId());
			getusercouponinfoesbypagingBean.setGetStartTime(batch.getValidStart());
			getusercouponinfoesbypagingBean.setGetEndTime(batch.getValidEnd());
			getusercouponinfoesbypagingBean.setCreaterType(batch.getCreaterType().intValue());
			getusercouponinfoesbypagingBean.setPageIndex(1);
			getusercouponinfoesbypagingBean.setPageSize(20);
			
			getusercouponinfoesbypagingCall.setData(getusercouponinfoesbypagingBean);
			getusercouponinfoesbypagingCall.callService();		
			
			Logger.verifyEquals("0", getusercouponinfoesbypagingCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", getusercouponinfoesbypagingCall.getMessage(), "验证返回Message");

			List<CouponInfo> couponInfos = getusercouponinfoesbypagingCall.getCouponInfos().getCouponInfoes();
			
			for (int i=0;i<couponInfos.size();i++) {
				CouponInfo couponInfo = couponInfos.get(i);
				
				// 验证返回数据正确性
				coupon coupon1 = new couponWapper().selectBycouponCode(couponInfo.getCouponCode()).get(0);
				
				Logger.verifyEquals(getusercouponinfoesbypagingBean.getUserId(),coupon1.getUserId(), "验证绑定表中UserId");
				Logger.verifyEquals(getusercouponinfoesbypagingBean.getCreaterType(),couponInfo.getBatchCreateType(), "验证绑定表中BatchCreateType");
				
				Date validStartTime = YMTDateUtil.getDateFromReturn(couponInfo.getValidStartTime());
				Date validEndTime = YMTDateUtil.getDateFromReturn(couponInfo.getValidEndTime());
				
				Logger.verifyEquals(false, coupon1.getAddTime().before(getusercouponinfoesbypagingBean.getGetStartTime()), "验证返回validStartTime在传入GetStartTime后");
				Logger.verifyEquals(false, coupon1.getAddTime().after(getusercouponinfoesbypagingBean.getGetEndTime()), "验证返回validEndTime在传入GetStartTime后");
				
				couponbatch batch1 = new couponbatchWapper().selectByPrimaryKey(coupon1.getBatchId());
				//YmtCouponValue value = new YmtCouponValueWapper().selectByiCouponSettingId(batch1.getiCouponSettingId()).get(0);
			
				// 验证返回数据的正确性
				Logger.verifyEquals(batch1.getBatchId(), couponInfo.getBatchId(), "验证返回batchId");
				Logger.verifyEquals(batch1.getBatchCode(), couponInfo.getBatchCode(), "验证返回batchCode");
				Logger.verifyEquals(0, couponInfo.getOrderMinAmount().compareTo(batch1.getMinOrderValue()), "验证返回orderMinAmount");
				Logger.verifyEquals(0, couponInfo.getCouponAmount().compareTo(batch1.getCouponValue()), "验证返回couponAmount");
				Logger.verifyEquals(batch1.getOperatorId(), couponInfo.getCreaterId(), "验证返回batchCode");
				
				Logger.comment("####batchid###:"+coupon1.getBatchId());
				int usedTime=coupon1.getIsUsed()==true?0:1;
				Logger.verifyEquals(usedTime, couponInfo.getRemainUseTimes(), "验证返回RemainUseTimes");
				
				SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
				Logger.verifyEquals(time.format(coupon1.getAddTime()).subSequence(0, 19), couponInfo.getAddTime().replace("T", " "), "验证返回RemainUseTimes");
		
				Logger.verifyEquals(batch1.getCouponName(), couponInfo.getBatchName(), "验证返回BatchName");
				Logger.verifyEquals(batch1.getCouponDes(), couponInfo.getBatchDes(), "验证返回BatchDes");
				
			}			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetUserCouponInfoesByPaging_002() {
		Logger.start(true,"获取用户优惠券信息-创建者类型:平台");
		try {
			int userId = TestDataManager.getRandomIntNum(8);
			CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
			CreateCouponBean createCouponBean = CouponServiceMCallServiceV2.setDefaultPlatformCreateCouponBeanForRecieve();
			CreateCouponCall createCouponCall= CouponServiceMCallServiceV2.createPlatformCoupon(createCouponBean);
			UserReceiveCouponCall userReceiveCouponCall = CouponServiceMCallServiceV2.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			
			Map batch = new couponbatchWapper().getCouponBatchByBatchCode(createCouponCall.getBatchCode()).get(0);
			
			Map coupon = new couponWapper().selectCouponInfoByBatchCode(batch.get("BatchCode").toString()).get(0);
		//	YmtCouponSetting setting = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			
			getusercouponinfoesbypagingBean.setUserId(Integer.parseInt(coupon.get("UserId").toString()));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			getusercouponinfoesbypagingBean.setGetStartTime(sdf.parse(batch.get("ValidStart").toString()));
			getusercouponinfoesbypagingBean.setGetEndTime(sdf.parse(batch.get("ValidEnd").toString()));
			getusercouponinfoesbypagingBean.setCreaterType(Integer.parseInt(batch.get("CreaterType").toString()));
			getusercouponinfoesbypagingBean.setPageIndex(1);
			getusercouponinfoesbypagingBean.setPageSize(20);
			
			getusercouponinfoesbypagingCall.setData(getusercouponinfoesbypagingBean);
			getusercouponinfoesbypagingCall.callService();		
			
			Logger.verifyEquals("0", getusercouponinfoesbypagingCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", getusercouponinfoesbypagingCall.getMessage(), "验证返回Message");

			List<CouponInfo> couponInfos = getusercouponinfoesbypagingCall.getCouponInfos().getCouponInfoes();
			
			for (int i=0;i<couponInfos.size();i++) {
				CouponInfo couponInfo = couponInfos.get(i);
				
				// 验证返回数据正确性
				coupon coupon1 = new couponWapper().selectBycouponCode(couponInfo.getCouponCode()).get(0);
				
				Logger.verifyEquals(getusercouponinfoesbypagingBean.getUserId(),coupon1.getUserId(), "验证绑定表中UserId");
				Logger.verifyEquals(getusercouponinfoesbypagingBean.getCreaterType(),couponInfo.getBatchCreateType(), "验证绑定表中BatchCreateType");
				
				Date validStartTime = YMTDateUtil.getDateFromReturn(couponInfo.getValidStartTime());
				Date validEndTime = YMTDateUtil.getDateFromReturn(couponInfo.getValidEndTime());
				
				Logger.verifyEquals(false, coupon1.getAddTime().before(getusercouponinfoesbypagingBean.getGetStartTime()), "验证返回validStartTime在传入GetStartTime后");
				Logger.verifyEquals(false, coupon1.getAddTime().after(getusercouponinfoesbypagingBean.getGetEndTime()), "验证返回validEndTime在传入GetStartTime后");
				
				couponbatch batch1 = new couponbatchWapper().selectByPrimaryKey(coupon1.getBatchId());
				//YmtCouponValue value = new YmtCouponValueWapper().selectByiCouponSettingId(batch1.getiCouponSettingId()).get(0);
			
				// 验证返回数据的正确性
				Logger.verifyEquals(batch1.getBatchId(), couponInfo.getBatchId(), "验证返回batchId");
				Logger.verifyEquals(batch1.getBatchCode(), couponInfo.getBatchCode(), "验证返回batchCode");
				Logger.verifyEquals(0, couponInfo.getOrderMinAmount().compareTo(batch1.getMinOrderValue()), "验证返回orderMinAmount");
				Logger.verifyEquals(0, couponInfo.getCouponAmount().compareTo(batch1.getCouponValue()), "验证返回couponAmount");
				Logger.verifyEquals(batch1.getOperatorId(), couponInfo.getCreaterId(), "验证返回batchCode");
				
				int usedTime=Boolean.parseBoolean(coupon.get("IsUsed").toString())==true?0:1;
				Logger.verifyEquals(usedTime, couponInfo.getRemainUseTimes(), "验证返回RemainUseTimes");
				
				SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
				Logger.verifyEquals(time.format(coupon1.getAddTime()).subSequence(0, 19), couponInfo.getAddTime().replace("T", " "), "验证返回RemainUseTimes");
		
				Logger.verifyEquals(batch1.getCouponName(), couponInfo.getBatchName(), "验证返回BatchName");
				Logger.verifyEquals(batch1.getCouponDes(), couponInfo.getBatchDes(), "验证返回BatchDes");
				
			}			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetUserCouponInfoesByPaging_003() {
		Logger.start(true,"获取用户优惠券信息-创建者类型:商家");
		try {
			int userId = TestDataManager.getRandomIntNum(8);
			CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
			
			SellerCreateBatchBean sellerCreateBatchBean = CouponServiceMCallServiceV2.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall createCouponCall= CouponServiceMCallServiceV2.SellerCreateBatch(sellerCreateBatchBean);
		
			UserReceiveCouponCall userReceiveCouponCall = CouponServiceMCallServiceV2.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			
			Map batch = new couponbatchWapper().getCouponBatchByBatchCode(createCouponCall.getBatchCode()).get(0);
			
			Map coupon = new couponWapper().selectCouponInfoByBatchCode(batch.get("BatchCode").toString()).get(0);
		//	YmtCouponSetting setting = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			
			getusercouponinfoesbypagingBean.setUserId(Integer.parseInt(coupon.get("UserId").toString()));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			getusercouponinfoesbypagingBean.setGetStartTime(sdf.parse(batch.get("ValidStart").toString()));
			getusercouponinfoesbypagingBean.setGetEndTime(sdf.parse(batch.get("ValidEnd").toString()));
			getusercouponinfoesbypagingBean.setCreaterType(Integer.parseInt(batch.get("CreaterType").toString()));
			getusercouponinfoesbypagingBean.setPageIndex(1);
			getusercouponinfoesbypagingBean.setPageSize(20);
			
			getusercouponinfoesbypagingCall.setData(getusercouponinfoesbypagingBean);
			getusercouponinfoesbypagingCall.callService();		
			
			Logger.verifyEquals("0", getusercouponinfoesbypagingCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", getusercouponinfoesbypagingCall.getMessage(), "验证返回Message");

			List<CouponInfo> couponInfos = getusercouponinfoesbypagingCall.getCouponInfos().getCouponInfoes();
			
			for (int i=0;i<couponInfos.size();i++) {
				CouponInfo couponInfo = couponInfos.get(i);
				
				// 验证返回数据正确性
				coupon coupon1 = new couponWapper().selectBycouponCode(couponInfo.getCouponCode()).get(0);
				
				Logger.verifyEquals(getusercouponinfoesbypagingBean.getUserId(),coupon1.getUserId(), "验证绑定表中UserId");
				Logger.verifyEquals(getusercouponinfoesbypagingBean.getCreaterType(),couponInfo.getBatchCreateType(), "验证绑定表中BatchCreateType");
				
				Date validStartTime = YMTDateUtil.getDateFromReturn(couponInfo.getValidStartTime());
				Date validEndTime = YMTDateUtil.getDateFromReturn(couponInfo.getValidEndTime());
				
				Logger.verifyEquals(false, coupon1.getAddTime().before(getusercouponinfoesbypagingBean.getGetStartTime()), "验证返回validStartTime在传入GetStartTime后");
				Logger.verifyEquals(false, coupon1.getAddTime().after(getusercouponinfoesbypagingBean.getGetEndTime()), "验证返回validEndTime在传入GetStartTime后");
				
				couponbatch batch1 = new couponbatchWapper().selectByPrimaryKey(coupon1.getBatchId());
				//YmtCouponValue value = new YmtCouponValueWapper().selectByiCouponSettingId(batch1.getiCouponSettingId()).get(0);
			
				// 验证返回数据的正确性
				Logger.verifyEquals(batch1.getBatchId(), couponInfo.getBatchId(), "验证返回batchId");
				Logger.verifyEquals(batch1.getBatchCode(), couponInfo.getBatchCode(), "验证返回batchCode");
				Logger.verifyEquals(0, couponInfo.getOrderMinAmount().compareTo(batch1.getMinOrderValue()), "验证返回orderMinAmount");
				Logger.verifyEquals(0, couponInfo.getCouponAmount().compareTo(batch1.getCouponValue()), "验证返回couponAmount");
				Logger.verifyEquals(batch1.getOperatorId(), couponInfo.getCreaterId(), "验证返回batchCode");
				
				int usedTime=Boolean.parseBoolean(coupon.get("IsUsed").toString())==true?0:1;
				Logger.verifyEquals(usedTime, couponInfo.getRemainUseTimes(), "验证返回RemainUseTimes");
				
				SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
				Logger.verifyEquals(time.format(coupon1.getAddTime()).subSequence(0, 19), couponInfo.getAddTime().replace("T", " "), "验证返回RemainUseTimes");
		
				Logger.verifyEquals(batch1.getCouponName(), couponInfo.getBatchName(), "验证返回BatchName");
				Logger.verifyEquals(batch1.getCouponDes(), couponInfo.getBatchDes(), "验证返回BatchDes");
				
			}			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}